package item.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import item.model.ItemInfo;
import item.dao.ItemDAO;
import board.action.PagingAction;
import board.action.WriteAction;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;

public class ListAction implements Action,Preparable,ModelDriven{
	static Logger logger = Logger.getLogger(WriteAction.class);
	ItemInfo itemInfo;
	ItemDAO itemDAO = ItemDAO.getInstance();
	
	private List<ItemInfo> list = new ArrayList<ItemInfo>();
	
	private int currentPage = 1;
	private int totalCount;
	private int blockCount = 10;
	private int blockPage = 5;
	private String pagingHtml;
	private PagingAction page;
	
	public String execute() throws Exception {
		totalCount = itemDAO.selectCnt(itemInfo.getCate());
		page = new PagingAction(currentPage,totalCount,blockCount,blockPage);
		pagingHtml = page.getPagingHtml().toString();
		Map map = new HashMap();
		map.put("cate", itemInfo.getCate());
		map.put("start", page.getStartCount());
		map.put("end", page.getEndCount());
		list = itemDAO.select(map);
		
		return SUCCESS;
	}
	
	public String topItem() throws Exception{
		list = itemDAO.selectTopItem();
		return SUCCESS;
	}
	
	public List<ItemInfo> getList() {
		return list;
	}

	public void setList(List<ItemInfo> list) {
		this.list = list;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public String getPagingHtml() {
		return pagingHtml;
	}

	public void setPagingHtml(String pagingHtml) {
		this.pagingHtml = pagingHtml;
	}

	public void prepare() throws Exception {
		itemInfo = new ItemInfo(); 
	}
	public Object getModel() {
		return itemInfo;
	}
}
